package barcode

import (
	"fmt"
	
	"inkjet168x-sdk-go/sdk/protocol/common"
)

// Builder 条码协议构建器 - 建造者模式
type Builder struct {
	protocol *Protocol
}

// NewBuilder 创建条码协议构建器
func NewBuilder() common.ProtocolBuilder {
	return &Builder{
		protocol: NewProtocol().(*Protocol),
	}
}

// SetText 设置条码内容
func (bb *Builder) SetText(text string) *Builder {
	bb.protocol.SetText(text)
	return bb
}

// SetBarcodeType 设置条码类型
func (bb *Builder) SetBarcodeType(barcodeType Type) *Builder {
	bb.protocol.SetBarcodeType(barcodeType)
	return bb
}

// SetPosition 设置位置
func (bb *Builder) SetPosition(x, y uint32) *Builder {
	bb.protocol.SetPosition(x, y)
	return bb
}

// SetSize 设置尺寸
func (bb *Builder) SetSize(width, height uint32) *Builder {
	bb.protocol.SetSize(width, height)
	return bb
}

// SetTextDisplay 设置文本显示
func (bb *Builder) SetTextDisplay(display bool) *Builder {
	bb.protocol.SetTextDisplay(display)
	return bb
}

// SetTextHeight 设置文本高度
func (bb *Builder) SetTextHeight(height uint32) *Builder {
	bb.protocol.TextHeight = height
	return bb
}

// SetSequence 设置序列号
func (bb *Builder) SetSequence(sequence uint16) *Builder {
	bb.protocol.Sequence = sequence
	return bb
}

// Build 构建协议实例
func (bb *Builder) Build() (common.Protocol, error) {
	// 验证参数
	if err := bb.protocol.Validate(); err != nil {
		return nil, fmt.Errorf("条码协议参数验证失败: %w", err)
	}
	
	// 返回协议实例的副本
	result := *bb.protocol
	return &result, nil
}

// Reset 重置构建器
func (bb *Builder) Reset() common.ProtocolBuilder {
	bb.protocol = NewProtocol().(*Protocol)
	return bb
}

// 便捷方法

// AsQRCode 设置为二维码
func (bb *Builder) AsQRCode() *Builder {
	return bb.SetBarcodeType(TypeQRCode)
}

// AsCode128 设置为Code128条码
func (bb *Builder) AsCode128() *Builder {
	return bb.SetBarcodeType(TypeCODE128)
}

// AsEAN13 设置为EAN13条码
func (bb *Builder) AsEAN13() *Builder {
	return bb.SetBarcodeType(TypeEAN13)
}

// AsEAN8 设置为EAN8条码
func (bb *Builder) AsEAN8() *Builder {
	return bb.SetBarcodeType(TypeEAN8)
}

// AsPDF417 设置为PDF417条码
func (bb *Builder) AsPDF417() *Builder {
	return bb.SetBarcodeType(TypePDF417)
}

// AsDataMatrix 设置为DataMatrix条码
func (bb *Builder) AsDataMatrix() *Builder {
	return bb.SetBarcodeType(TypeDataMatrix)
}

// WithText 设置显示文本
func (bb *Builder) WithText() *Builder {
	return bb.SetTextDisplay(true)
}

// WithoutText 设置不显示文本
func (bb *Builder) WithoutText() *Builder {
	return bb.SetTextDisplay(false)
}

// Small 设置为小尺寸
func (bb *Builder) Small() *Builder {
	return bb.SetSize(100, 100)
}

// Medium 设置为中等尺寸
func (bb *Builder) Medium() *Builder {
	return bb.SetSize(150, 150)
}

// Large 设置为大尺寸
func (bb *Builder) Large() *Builder {
	return bb.SetSize(200, 200)
}